import baseCofig from '../config.js'
import utils from "@/sdk/tool/utils.js"
var gotologin = false;
var platform = baseCofig.platform;

const http = {
	//post请求
	post(url, data, loading, getData, header) {
		url = baseCofig.request_url + url;
		return http.request(url, data, "POST", header, loading, getData);
	},
	//get请求
	get(url, data, loading, getData, header) {
		url = baseCofig.request_url + url;
		return http.request(url, data, "GET", header, loading, getData);
	},
	//同步post请求
	async asyncPost(url, data, getData, header) {
		url = baseCofig.request_url + url;
		return await http.request(url, data, "POST", header, false, getData);
	},
	//同步get请求
	async asyncGet(url, data, getData, header) {
		url = baseCofig.request_url + url;
		return await http.request(url, data, "GET", header, false, getData);
	},
	//上传文件
	/**
	 * @param {Object} url 请求地址
	 * @param {Object} callback 回调函数
	 * @param {Object} config 配置
	 */
	upFile(url, callback, config) {
		if (config == undefined) {
			config = {
				count: 1,
				sourceType: ['camera', 'album']
			}
		}
		config.success = function(res) {
			var tempFilePaths = res.tempFilePaths;
			let header = config.header || {}
			var token = uni.getStorageSync('token');
			if (token) {
				header.token = token
			}
			for (var i in tempFilePaths) {
				// console.log(i);
				uni.uploadFile({
					url: baseCofig.request_url + url,
					filePath: tempFilePaths[i],
					name: config.name || 'file',
					formData: config.formData || {},
					header: header,
					success(response) {
						if (response.statusCode == 200) {
							if (callback) {
								callback(response.data)
							}
						}
					},
					fail(res) {
						console.log("上传失败", res);
					}
				})
			}
		}
		config.fail = function(re) {
			console.log("读取文件失败", re);
		}
		console.log(config);
		uni.chooseImage(config)
	},
	//总请求
	request(url, data, method, header, loading, getData) {
		method = method || "GET";
		data = data || {};
		header = header || {};
		if (loading == undefined) {
			loading = true;
		}
		if (getData == undefined) {
			getData = true;
		}
		if (baseCofig.debug) {
			console.log(data);
		}
		let token = uni.getStorageSync("token")
		if (token) {
			header.token = token
		}
		header.platform = platform
		// #ifdef H5
		header.baseURL = "/"
		// #endif
		
		return new Promise((resolve, reject) => {
			console.log('lonading',loading,'-------------');
			if (loading === true) {
				uni.showLoading({
					title: "加载中",
					mask:true
				})
			}
			uni.request({
				url: url,
				header: header,
				method: method,
				data: data,
				success(res) {
					if (baseCofig.debug.debug) {
						console.log(res);
					}
					if (res.data.code == baseCofig.request_success) {
						if (getData) {
							resolve(res.data.data)
						} else {
							resolve(res.data)
						}
					} else if(res.data.code == baseCofig.not_login_fail){
						// #ifdef H5
						uni.showToast({
							icon: "none",
							title: res.data.msg
						})
						// #endif
						// #ifdef MP
						uni.showModal({
							title: "提示",
							content: res.data.msg,
							showCancel: false,
						})
						// #endif
						// console.log(gotologin);
						if(gotologin==true){
							return
						}
						console.log('------------');
						gotologin = true;
						setTimeout(function(){
							uni.navigateTo({
								url:"/pages/user/login?back=1",
								success(){
									setTimeout(function(){
										gotologin = false;
									},3000)
								},
								fail(){
									gotologin = false
								}
							})
						},1000)
					}else{
						// #ifdef H5
						uni.showToast({
							icon: "none",
							title: res.data.msg
						})
						// #endif
						// #ifdef MP
						uni.showModal({
							title: "提示",
							content: res.data.msg,
							showCancel: false,
						})
						// #endif
					}
				},
				fail(res) {
					console.log(res);
					if (baseCofig.debug.debug) {
						console.log(res);
					}
					reject(res);
				},
				complete() {
					if (loading === true) {
						uni.hideLoading();
					}
				}
			})
		})
	}
}
export default http